package com.guangfeng.yql.mysql.generator;

import cn.hutool.db.handler.BeanListHandler;
import cn.hutool.db.handler.StringHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.guangfeng.yql.comm.controller.BaseController;
import com.guangfeng.yql.mysql.engine.AlibabaVelocityTemplateEngine;
import com.guangfeng.yql.mysql.enumerate.CustomEnum;
import lombok.SneakyThrows;
import org.springframework.boot.jdbc.DataSourceBuilder;

import java.sql.Connection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class Generator {
    @SneakyThrows
    public static void main(String[] args) {

        DataSourceBuilder<?> sourceBuilder = DataSourceBuilder.create();
        sourceBuilder.url("jdbc:mysql://60.190.128.190:13316/yql_mall?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true");
        sourceBuilder.username("yql");
        sourceBuilder.password("123456");
        Connection connection = sourceBuilder.build().getConnection();
        List<String> query = SqlExecutor.query(connection, "show tables;", new BeanListHandler<>(String.class));
        for (String tableName : query) {
            FastAutoGenerator.create("jdbc:mysql://60.190.128.190:13316/yql_mall?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true", "yql", "123456")
                    .globalConfig(builder -> {
                        builder.author("yql") // 设置作者
                                .enableSwagger() // 开启 swagger 模式
                                .fileOverride() // 覆盖已生成文件
                                .outputDir("D:\\Project\\yql_mall\\"); // 指定输出目录
                    })
                    .packageConfig(builder -> {
                        builder.parent("com.guangfeng") // 设置父包名
                                .controller("controller")
                                .service("service")
                                .mapper("mysql")
                                .entity("bean")
                                .other("bean")
                                .moduleName("yql") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\Project\\yql_mall\\yql-mail-mysql\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                    })
                    .strategyConfig(builder -> {

                        builder.addInclude(tableName) // 设置需要生成的表名
                                .addTablePrefix("yql_", "sys_")
                                .entityBuilder()
                                .enableLombok()
                                .enableChainModel()
                                .logicDeleteColumnName("del_flag")
                                .logicDeletePropertyName("delFlag")
                                .addTableFills(new Column("create_time", FieldFill.INSERT))
                                .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                                .enableRemoveIsPrefix()
                                .enableTableFieldAnnotation()
                                .build()
                                .mapperBuilder()
                                .enableBaseResultMap().enableBaseColumnList().enableMapperAnnotation()
                                .build().controllerBuilder().enableRestStyle().enableHyphenStyle()
                                .superClass(BaseController.class)
                                .build()
                        ; // 设置过滤表前缀

                    })
                    .injectionConfig(builder -> {
                        HashMap<String, String> map = new HashMap<>();
                        map.put(CustomEnum.PO.getModel(), CustomEnum.PO.getPath());
                        map.put(CustomEnum.VO.getModel(), CustomEnum.VO.getPath());
                        map.put(CustomEnum.DTO.getModel(), CustomEnum.DTO.getPath());
                        builder.customFile(map);
                    })
                    .templateEngine(new AlibabaVelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                    .execute();
        }


    }
}
